package elijah.eupdater;

import java.util.concurrent.TimeUnit;

import elijah.eupdater.bean.Result;
import elijah.eupdater.bean.TerminalDevice;
import io.reactivex.Single;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;
import retrofit2.http.Streaming;
import retrofit2.http.Url;

public interface NetApi {
    @POST("eupdater/terminal/updateTerminal")
    Single<Result<TerminalDevice>> updateTerminalInfo(@Body TerminalDevice terminalInfo);

    @Streaming
    @GET
    Call<ResponseBody> downloadFile(@Url String url);


    @GET("eupdater/terminal/startUpdate")
    Single<Result> startUpdate(@Query("deviceId") String deviceId, @Query("packageName") String packageName);

    @GET("eupdater/terminal/endUpdate")
    Single<Result> endUpdate(@Query("deviceId") String deviceId, @Query("packageName") String packageName);

    class Creator {
        public static NetApi create(String host) {
            OkHttpClient build = new OkHttpClient.Builder()
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .build();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(host)
                    .client(build)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
            return retrofit.create(NetApi.class);
        }
    }


}
